# makefile, written by guido socher
#
include ../Makevars.mk
#
LOADARG=-p $(DUDECPUTYPE) -c arduino  -e -U  flash:w:


#this baudrate doesn't seem to speed up upload at all. TODO, find out why
#LOADARG=-p $(DUDECPUTYPE) -c arduino -b 115200 -e -U  flash:w:
# 
#
CC=avr-gcc
OBJCOPY=avr-objcopy
# optimize for size:
CFLAGS=-g -mmcu=$(MCU) -Wall -Wstrict-prototypes -Os -mcall-prologues
#-------------------
.PHONY: all main
#


main: main.hex
#


#DEPENDS#
websrv_help_functions.o: ../websrv_help_functions.c ../websrv_help_functions.h ip_config.h 
	$(CC) $(CFLAGS) -I. -Os -c ../websrv_help_functions.c
enc28j60.o: ../enc28j60.c ../timeout.h ../enc28j60.h
	$(CC) $(CFLAGS) -I. -Os -c ../enc28j60.c
uart.o: uart.c 
	$(CC) $(CFLAGS) -I. -Os -c uart.c
ip_arp_udp_tcp.o: ../ip_arp_udp_tcp.c ../net.h ../enc28j60.h ip_config.h
	$(CC) $(CFLAGS) -I. -Os -c ../ip_arp_udp_tcp.c
basic_web_server.o: basic_web_server.c ../ip_arp_udp_tcp.h ../enc28j60.h ../timeout.h ../net.h ../websrv_help_functions.h ip_config.h
	$(CC) $(CFLAGS) -I. -Os -c basic_web_server.c

#DHCP
dhcp_client.o: ../dhcp_client.c  ../dhcp_client.h ../net.h ../enc28j60.h ../ip_arp_udp_tcp.h ip_config.h
	$(CC) $(CFLAGS) -I. -Os -c ../dhcp_client.c
dnslkup.o: ../dnslkup.c  ../dnslkup.h ../ip_arp_udp_tcp.h ../net.h ip_config.h
	$(CC) $(CFLAGS) -I. -Os -c ../dnslkup.c


#MAIN#
main.hex: main.elf 
	$(OBJCOPY) -R .eeprom -O ihex main.elf main.hex 
	avr-size main.elf
	@echo " "
	@echo "Expl.: data=initialized data, bss=uninitialized data, text=code"
	@echo " "

main.elf: main.o ip_arp_udp_tcp.o enc28j60.o websrv_help_functions.o uart.o dhcp_client.o dnslkup.o
	$(CC) $(CFLAGS) -o main.elf -Wl,-Map,main.map main.o ip_arp_udp_tcp.o enc28j60.o websrv_help_functions.o uart.o dnslkup.o dhcp_client.o
main.o: main.c ../ip_arp_udp_tcp.h ../enc28j60.h ../timeout.h ../net.h ../websrv_help_functions.h ip_config.h uart.h ../dnslkup.h ../dhcp_client.h
	$(CC) $(CFLAGS) -I. -Os -c main.c



#LOAD#
#------------------
load: main.hex
	$(LOADCMD) $(LOADARG)main.hex
install: main.hex
	$(LOADCMD) $(LOADARG)main.hex










#IGNORE#
#-------------------
# Check this with make rdfuses
# 
rdfuses:
	$(LOADCMD) -p $(DUDECPUTYPE) -c stk500v2 -v -q
#
fuse:
	@echo "Setting clock source to external clock on pin xtal1"
	$(LOADCMD) -p  $(DUDECPUTYPE) -c stk500v2 -u -v -U lfuse:w:0x60:m
#
fuses:
	@echo "Setting clock source to external clock on pin xtal1"
	$(LOADCMD) -p  $(DUDECPUTYPE) -c stk500v2 -u -v -U lfuse:w:0x60:m
#
#-------------------
clean:
	rm -f *.o *.map *.elf basic_web_server.hex  test_OKworks.hex  test_readSiliconRev.hex main.hex
#-------------------
